/**
 * Created by Administrator on 2017/7/24.
 */
var userIdCard = new Object();
var page;
var company_name;
var page_size = 10;
var tableMsg = "success";
// 默认未打开驱动
var ifOpen = false;
var companyId ;
var departmentId ;
var departmentName ;

var timeOut;

// 初始化
userIdCard.init = function (_page) {
    page = _page;
    companyId = $("#companyId").val();
    departmentId = $("#departmentId").val();
    departmentName = $("#departmentName").val();
};

userIdCard.initTable = function() {
    var list_url = appPath + "/hardware/flushIdCards";
    var params = {
        'companyId': companyId,
        'departmentId': departmentId,
        'pageSize': 10,
        'pageNum': 1
    };
    $.ajax({
        url: list_url,
        data: params,
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            tableMsg = "success";
            var inner = "", item;
            var result = data['result'];
            if (!result) {
                inner = "<tr><td>暂无数据</td></tr>";
            }
            var dataList = result['dataList'];
            if (!dataList) {
                inner = "<tr><td>暂无数据</td></tr>";
            }
            var len = dataList.length;
            if(len > 0){
                // 组装数据
                for(var i=0; i< len; i++) {
                    item = dataList[i];
                    // 组装数据
                    if(item.msg != "success") {
                        tableMsg = item.msg;
                        continue;
                    }
                    inner += "<tr>";
                    inner += "<td>"+(parseInt(i)+1)+"</td>";
                    inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.Name+"</span></td>";
                    inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.Sex+"</span></td>";
                    inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.Nation+"</span></td>";
                    inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.IDCardNo+"</span></td>";
                    inner += "<td><span class=\"text-orange tooltip\" data-length='30'>"+item.Address+"</span></td>";
                    inner += "</tr>";
                }
                $("#idCard_tbody").html(inner);
            }


            if(tableMsg != "success"){
                if(tableMsg != "1" && tableMsg != "2" && tableMsg != "3"){
                    layer.confirm('提示：'+tableMsg, {
                        btn: ['确定'] //按钮
                    }, function(i){
                        layer.close(i);
                    });
                }
                //清除msg!=success的身份证信息
                userIdCard.clearErrRedis(tableMsg);
                if(tableMsg == "1"){
                    clearTimeout(timeOut);
                    layer.msg("请将身份证放置在设备感应区域，并点击录入按钮",{time:2000});
                    ifOpen = false;
                    return;
                }else if(tableMsg == "2"){
                    clearTimeout(timeOut);
                    layer.msg("请确认读卡器是否安装或打开",{time:2000});
                    ifOpen = false;
                    return;
                }else if(tableMsg == "3"){
                    clearTimeout(timeOut);
                    layer.confirm("读卡设备已关闭。如需录入，请重新点击录入按钮",{
                        btn: ['确定'] //按钮
                    }, function(i) {
                        ifOpen = false;
                        layer.close(i);
                    });
                    return;
                }else{

                }
            }else{
                // 5秒钟拉一次数据
                timeOut = setTimeout(function () {
                    userIdCard.initTable();
                }, 3000);
            }
        }
    });
};

/**
 * 申请打开身份证录入硬件
 */
userIdCard.idCardEntry = function () {
    $("#tishi").show();
    if(ifOpen == false){{
        ifOpen = true;
        // BosafeIDCard://{'companyId':'${companyId}','departmentId':'${departmentId}'}
        window.open("BosafeIDCard://{'companyId':'"+companyId+"','departmentId':'"+departmentId+"'}","_top");
        // 刷身份证信息
        userIdCard.initTable();
    }


    }



    /*var param= {'companyId':companyId, 'departmentId':departmentId, 'departmentName': departmentName};
    var params = {
        // 'urlStr': 'http://10.36.1.149:8080/hardware/getCardInfo',
        'urlStr': $("#receiveCardInfoUrl").val(),
        'param': JSON.stringify(param)
    };
    $.ajax({
        // url: "http://10.36.1.149:8888/IDCard/open",
        url: $("#openCardUrl").val(),
        async: false,
        type: 'post',
        data: params,
        success: function(result){
            var msg = result.meta.msg;

            // 设备打开失败
            if("设备打开失败" != msg){
                $("#tishi").show();
                // 刷身份证信息
                userIdCard.initTable();
            }else{
                $("#tishi").hide();
                layer.msg(msg);
            }
        }
    });*/
};




/**
 * 清除msg信息错误的证件信息
 */
userIdCard.clearErrRedis = function (tableMsg) {
    $.ajax({
        url: appPath + "/hardware/clearErrCards",
        data: {
            'companyId': companyId,
            'departmentId': departmentId
        },
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            if(data.result == "success"){
                if(tableMsg != "1" && tableMsg != "2" && tableMsg != "3"){
                    userIdCard.initTable();
                }
            }
        }
    });
};


/**
 * 点击保存按钮，批量保存人员信息
 */
userIdCard.saveInfo = function () {
    clearTimeout()
    if($("#idCard_tbody").html() == ''){
        layer.msg("暂无录入信息, 请先刷身份证",{icon: 3});
        return;
    }
    layer.confirm("您是否确认保存所录入的人员?", {icon: 3}, function (index) {
        clearTimeout(timeOut);
        var param= {'companyId':companyId, 'departmentId':departmentId, 'departmentName': departmentName};
        $.ajax({
            url:  appPath + "/hardware/saveCardInfos",
            async: false,
            type: 'post',
            data: param,
            success: function(data){
                if(data.result == "success"){
                    // 录入完成，默认账号为身份证号码，密码为身份证后6位!
                    layer.alert('录入完成，默认账号为身份证号码，密码为身份证后6位', {time : 3000});
                    window.history.go(-1);
                }else{
                    layer.msg(data.result);
                }
            }
        });
    }, function (index) {
        layer.close(index);
        return
    });
};


